home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / ha0999beta / src / misc.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  6KB  |  232 lines

  1. /***********************************************************************
  2.   This file is part of HA, a general purpose file archiver.
  3.   Copyright (C) 1995 Harri Hirvola
  4.  
  5.   This program is free software; you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation; either version 2 of the License, or
  8.   (at your option) any later version.
  9.  
  10.   This program is distributed in the hope that it will be useful,
  11.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.   GNU General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU General Public License
  16.   along with this program; if not, write to the Free Software
  17.   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ************************************************************************
  19.     HA miscalneous routines
  20. ***********************************************************************/
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include "ha.h"
  27. #include "error.h"
  28.  
  29. typedef void (*Voidfunc)(void);
  30.      
  31. struct culist {
  32.     union {
  33.     Voidfunc func;
  34.     struct {
  35.         char *name;
  36.         int handle;
  37.     } fileinfo;
  38.     } arg;
  39.     struct culist *prev,*next;
  40.     unsigned char flags;
  41. };
  42.  
  43. int skipemptypath=0,sloppymatch=1;
  44. static struct culist cuhead={{NULL},NULL,NULL,0}; 
  45. char **patterns;
  46. unsigned patcnt;
  47.  
  48. /**********************************************************************
  49.   Size test
  50. */
  51.  
  52. void testsizes(void) {
  53.  
  54.     if (sizeof(U16B)!=2) error(0,ERR_SIZE,"U16B");
  55.     if (sizeof(S16B)!=2) error(0,ERR_SIZE,"S16B");
  56.     if (sizeof(U32B)!=4) error(0,ERR_SIZE,"U32B");
  57.     if (sizeof(S32B)!=4) error(0,ERR_SIZE,"S32B");
  58. }
  59.  
  60.  
  61. /**********************************************************************
  62.     Cleanup routines
  63. */
  64.  
  65. void *cu_add(unsigned char flags, ...) {
  66.  
  67.     struct culist *ptr,*mark;
  68.     va_list vaptr;
  69.     char *string;
  70.     
  71.     mark=cuhead.next;
  72.     ptr=malloc(sizeof(struct culist));
  73.     if (ptr==NULL) error(1,ERR_MEM,"add_cleanup()");
  74.     ptr->flags=flags;
  75.     va_start(vaptr,flags);
  76.     if (flags&CU_FUNC) ptr->arg.func=va_arg(vaptr,Voidfunc);
  77.     else if ((flags&CU_RMFILE) || (flags&CU_RMDIR)) {
  78.     string=va_arg(vaptr,char *);
  79.     if ((ptr->arg.fileinfo.name=malloc(strlen(string)+1))==NULL) 
  80.       error(0,ERR_MEM,"cu_add()");
  81.     strcpy(ptr->arg.fileinfo.name,string);
  82.     if (!(flags&CU_RMDIR)) ptr->arg.fileinfo.handle=va_arg(vaptr,int);
  83.     }
  84.     va_end(vaptr);
  85.     ptr->next=cuhead.next;
  86.     if (cuhead.next!=NULL) cuhead.next->prev=ptr;
  87.     ptr->prev=&cuhead;
  88.     cuhead.next=ptr;
  89.     return mark;
  90. }
  91.  
  92. void *cu_getmark(void) {
  93.  
  94.     return cuhead.next;
  95. }
  96.  
  97. void cu_relax(void *mark) {
  98.  
  99.     struct culist *ptr;
  100.     
  101.     for (ptr=cuhead.next;ptr!=NULL && ptr!=mark;ptr=ptr->next) {
  102.     if (ptr->flags&CU_CANRELAX) ptr->flags|=CU_RELAXED; 
  103.     }
  104. }
  105.  
  106. void cu_do(void *mark) {
  107.  
  108.     struct culist *ptr;
  109.     
  110.     for (ptr=cuhead.next;ptr!=NULL && ptr!=mark;) {
  111.     if ((ptr->flags&CU_FUNC) && ptr->arg.func!=NULL) {
  112.         if (!(ptr->flags&CU_RELAXED)) ptr->arg.func();
  113.     }
  114.     else if ((ptr->flags&CU_RMFILE) && ptr->arg.fileinfo.name!=NULL) {
  115.         close(ptr->arg.fileinfo.handle);
  116.         if (!(ptr->flags&CU_RELAXED) && remove(ptr->arg.fileinfo.name)<0) {
  117.         error(0,ERR_REMOVE,ptr->arg.fileinfo.name);
  118.         }
  119.         free(ptr->arg.fileinfo.name);
  120.     }
  121.     else if ((ptr->flags&CU_RMDIR) && ptr->arg.fileinfo.name!=NULL) {
  122.         if (!(ptr->flags&CU_RELAXED)) rmdir(ptr->arg.fileinfo.name);
  123.         free(ptr->arg.fileinfo.name);
  124.     }
  125.     cuhead.next=ptr->next;
  126.     free(ptr);
  127.     ptr=cuhead.next;
  128.     }
  129. }
  130.  
  131. /**********************************************************************
  132.     Simple path handling
  133. */
  134.  
  135. char *getname(char *fullpath) {
  136.  
  137.     int i,j;
  138.     static char *name=NULL;
  139.     
  140.     if (name!=NULL) free(name),name=NULL;
  141.     for (i=j=strlen(fullpath);--i>=0;) {
  142.     if ((unsigned char)fullpath[i]==0xff) {
  143.         if ((name=malloc(j-i))==NULL) error(1,ERR_MEM,"getname()");
  144.         strcpy(name,fullpath+i+1);
  145.         return name;
  146.     }
  147.     }
  148.     return fullpath;
  149. }
  150.  
  151. char *getpath(char *fullpath) {
  152.  
  153.     int i;
  154.     static char *path=NULL;
  155.     
  156.     if (path!=NULL) free(path),path=NULL;
  157.     for(i=strlen(fullpath);--i;) {
  158.     if ((unsigned char)fullpath[i]==0xff) {
  159.         if ((path=malloc(i+2))==NULL) error(1,ERR_MEM,"getpath()");
  160.         strncpy(path,fullpath,i+1);
  161.         path[i+1]=0;
  162.         return path;
  163.     }
  164.     }
  165.     return "";
  166. }
  167.  
  168. char *fullpath(char *path, char *name) {
  169.  
  170.     static char *fullpath=NULL;
  171.     int need_delim;
  172.     
  173.     if (fullpath!=NULL) free(fullpath),fullpath=NULL;
  174.     if (path==NULL || *path==0) return name;
  175.     if ((unsigned char)path[strlen(path)-1]!=0xff) need_delim=1;
  176.     else need_delim=0;
  177.     if ((fullpath=malloc(strlen(path)+strlen(name)+need_delim+1))==NULL) {
  178.     error(1,ERR_MEM,"fullpath()");
  179.     }            
  180.     strcpy(fullpath,path);
  181.     strcpy(fullpath+strlen(fullpath)+need_delim,name);
  182.     if (need_delim) fullpath[strlen(fullpath)]=0xff;
  183.     return fullpath;
  184. }
  185.  
  186. void makepath(char *hapath) {
  187.  
  188.     char *last,*path;
  189.     
  190.     for (last=strchr(hapath,0xff);last!=NULL;last=strchr(last+1,0xff)) {
  191.     *last=0;
  192.     if (access((path=md_tomdpath(hapath)),F_OK)) {
  193.         if (mkdir(path,DEF_DIRATTR)<0) error(0,ERR_MKDIR,path);
  194.     }
  195.     *last=0xff;
  196.     }    
  197. }
  198.  
  199.  
  200. /**********************************************************************
  201.     General filename matching (for paths in ha format !)
  202. */
  203.  
  204. static int matchpattern(char *matchpath, char *matchpat, 
  205.             char *path, char *name) {
  206.  
  207.     if (((*matchpath==0 && skipemptypath && sloppymatch) || 
  208.      strcmp(matchpath,path)==0) &&
  209.     md_namecmp(matchpat,name)) return 1;
  210.     return 0;
  211. }
  212.  
  213.  
  214. int match(char *path, char *name) {
  215.  
  216.     int i;
  217.     char *fullhapath;
  218.     
  219.     for (i=0;i<patcnt;++i) {
  220.     fullhapath=md_tohapath(patterns[i]);
  221.     if (matchpattern(getpath(fullhapath),getname(fullhapath),
  222.              md_strcase(path),md_strcase(name))) return 1;    
  223.     }    
  224.     return 0;    
  225. }
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.